N Queens

Hard

Question

Given a square chessboard of n width and height, place n queens on the board such that they do not attack each other (a queen cannot be on the same line horizontally, vertically, or diagonally as another queen).

Return all possible solutions to the n queens problem.

Note: In our solution, "Q" indicates a queen while "." indicates an empty space.

Input: n = 4

Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]

There are two distinct solutions to the 4-queens puzzle:

N Queens Example 1

Input: n = 1

Output: [["Q"]]

There is a single solution to the 1-queen puzzle:

N Queens Example 2

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

How many unique combinations should we return if given n = 2?
0
1
2
4

Login or signup to save your code.

Notes